package com.example.foobar.controller;

import com.example.config.QinNiuParam;
import com.example.foobar.dao.service.FooBarService;
import com.example.foobar.entity.FooBar;
import com.example.foobar.param.FooBarPageParam;
import com.qiniu.storage.DownloadUrl;
import com.qiniu.util.Auth;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.core.validator.groups.Update;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * FooBar 控制器
 *
 * @author geekidea
 * @since 2021-05-31
 */
@Slf4j
@RestController
@RequestMapping("/qiniu")
@Module("qiniu")
@Api(value = "QiNiu", tags = {"七牛相关接口"})
public class QiniuController extends BaseController {

    @Autowired
    QinNiuParam qinNiuParam;
    /**
     * FooBar分页列表
     */
    @GetMapping("/getQiNiuToken")
    @OperationLog(name = "获取七牛token", type = OperationLogType.PAGE)
    @ApiOperation(value = "获取七牛token", response = FooBar.class)
    public ApiResult<String> getQiNiuToken() throws Exception {
        Auth auth = Auth.create(qinNiuParam.getAccessKey(), qinNiuParam.getSecretKey());
        String upToken = auth.uploadToken(qinNiuParam.getBucket(),null, qinNiuParam.getExpires(), null);
        return ApiResult.ok(upToken);
    }

    @GetMapping("/getDownloadUrl")
    @OperationLog(name = "获取七牛下载链接", type = OperationLogType.PAGE)
    @ApiOperation(value = "获取七牛下载链接", response = FooBar.class)
    public ApiResult<String> getDownloadUrl() throws Exception {
        DownloadUrl url = new DownloadUrl("qn.benz1.cn", false, "1627147809090.mp4");
//        url.setAttname("1627147809090.mp4") // 配置 attname
//                .setFop(fop) // 配置 fop
//                .setStyle(style, styleSeparator, styleParam)
        String s = url.buildURL();
        return ApiResult.ok(s);
    }

}

